package com.lbmb.photodrawaimanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbmb.photodrawaimanager.entity.dto.VideoEntity;
import com.lbmb.photodrawaimanager.entity.req.VideoReq;
import com.lbmb.photodrawaimanager.entity.resp.VideoUserResp;
import com.lbmb.photodrawaimanager.mapper.VideoMapper;
import com.lbmb.photodrawaimanager.service.VideoService;
import com.lbmb.photodrawaimanager.utils.OssUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/31:06 上午
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, VideoEntity> implements VideoService {


    @Autowired
    OssUtils ossUtils;
    @Autowired
    VideoMapper videoMapper;

    @Override
    public Boolean uploadVideo(MultipartFile file, MultipartFile imgFile, VideoReq req) {
        String fileUrl = ossUtils.uploadOneFile(file);
        String imgFileUrl = ossUtils.uploadOneFile(imgFile);
        // 获取格式
        String fileFix = fileUrl.substring(fileUrl.lastIndexOf(".") + 1);
        VideoEntity videoEntity = new VideoEntity();
        BeanUtils.copyProperties(req, videoEntity);
        videoEntity.setFileFix(fileFix);
        videoEntity.setFileUrl(fileUrl);
        videoEntity.setStatus(1);
        videoEntity.setImgUrl(imgFileUrl);
        videoEntity.setAccountType(1);
        return videoMapper.insert(videoEntity) > 1;
    }

    @Override
    public IPage<VideoUserResp> queryAllVideo(VideoReq req) {
        Page<VideoEntity> page = new Page(req.getPageNum(), req.getPageSize());
        // Page<VideoUserResp> page = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<VideoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VideoEntity::getDeleted, 0);
        if (req.getModuleType() != null) {
            queryWrapper.eq(VideoEntity::getVideoType, req.getModuleType());
        }
        if (req.getModuleName() != null) {
            queryWrapper.like(VideoEntity::getVideoType, req.getModuleName());
        }

        queryWrapper.like(VideoEntity::getTitleId, req.getTitleId());
        if (req.getAccountType() != null) {
            queryWrapper.like(VideoEntity::getAccountType, req.getAccountType());
        }
        if (req.getOrderBy() == null) {
            if (req.getOrderByDesc()) {
                queryWrapper.orderByDesc(VideoEntity::getCreateDate);
            } else {
                queryWrapper.orderByAsc(VideoEntity::getCreateDate);
            }
        } else if (req.getOrderBy() == 1) {
            if (req.getOrderByDesc()) {
                queryWrapper.orderByDesc(VideoEntity::getPeopleCount);
            } else {
                queryWrapper.orderByAsc(VideoEntity::getPeopleCount);
            }
        }
        IPage<VideoEntity> result = videoMapper.selectPage(page, queryWrapper);
        IPage<VideoUserResp> convert = result.convert(a -> {
            VideoUserResp videoUserResp = new VideoUserResp();
            BeanUtils.copyProperties(a, videoUserResp);
            return videoUserResp;
        });
        return convert;
    }

    @Override
    public VideoUserResp queryVideoById(Long id) {
        VideoEntity videoEntity = videoMapper.selectById(id);
        VideoUserResp videoUserResp = new VideoUserResp();
        BeanUtils.copyProperties(videoEntity, videoUserResp);
        return videoUserResp;
    }
}
